# -*- coding: utf-8 -*-
# Project : flacon
# File : flac_encoder.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

import encoder
from settings import *
from PyQt4 import QtCore

from flaconglobal import *
import flac_config

class FlacEncoder(encoder.Encoder):
    id = 'FLAC'
    extension = 'flac'
    name = 'Flac'


   #****************************************************************
   #
   #****************************************************************
    def getEncoderArgs(self):
        args = QStringList()
        args << settings.value("Programs/flac").toString()
        args << "--force"       # Force overwriting of output files.
        args << "--silent"      # Suppress progress indicator

        # Settings .................................................
        args << QString("--compression-level-%1").arg(settings.value("Flac/Compression").toString())

        # Tags .....................................................
        if (self._track.artist()): args << "--tag" << QString("artist=%1").arg(self._track.artist())
        if (self._track.album()):  args << "--tag" << QString("album=%1").arg(self._track.album())
        if (self._track.genre()):  args << "--tag" << QString("genre=%1").arg(self._track.genre())
        if (self._track.year()):   args << "--tag" << QString("date=%1").arg(self._track.year())
        if (self._track.title()):  args << "--tag" << QString("title=%1").arg(self._track.title())
        if (self._track.discId()): args << "--tag" << QString("discId=%1").arg(self._track.discId())
        if (self._track.title()):  args << "--tag" << QString("comment=%1").arg(self._track.comment())
        args << "--tag" << QString("TRACKNUMBER=%1").arg(self._track.trackNum())
        args << "--tag" << QString("TOTALTRACKS=%1").arg(self._track.trackSet().count())
        args << "--tag" << QString("TRACKTOTAL=%1").arg(self._track.trackSet().count())

        args << "-"
        args << "-o" << self._outFile,

        return args


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def trackGainClass(cls):
        return FlacGain


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def configPageClass(cls):
        return flac_config.FlacConfig


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def programs(cls):
        return ['flac']


class FlacGain(encoder.Gain):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, disc, trackIndexses):
        encoder.Gain.__init__(self, disc, trackIndexses)


   #****************************************************************
   #
   #****************************************************************
    def getArgs(self):
        args = QStringList()
        args << settings.value("Programs/metaflac").toString()
        args << "--add-replay-gain"

        args << self._files

        return args


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def check(cls, project):
        encoder.Encoder.checkPrograms(cls.programs())

        for disk in project:
            if disk.audioFile().sampleRate() > 48000:
                raise errors.ConvertError(QtGui.qApp.translate('FlacGain', "you can't use 'ReplayGain' for files with sample rates above 48kHz. Metaflac doesn't support such files."))


   #****************************************************************
   #
   #****************************************************************
    @classmethod
    def programs(cls):
        return ['metaflac']


#****************************************************************
# Register encoder & gain classes
#****************************************************************
encoder.registerEncoder(FlacEncoder)
